<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: sitemap.xml.php 212 2013-09-22 08:00:26Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 212 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 08:00:26 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

function iso8601_date($timestamp) {
	if (PHP_VERSION < 5) {
		$tzd = date('O',$timestamp);
		$tzd = substr(chunk_split($tzd, 3, ':'),0,6);
		return date('Y-m-d\TH:i:s', $timestamp) . $tzd;
	} else {
		return date('c', $timestamp);
	}
}

function rv_get_path($cat_id, $code) {
	global $cat_array;
	$my_cat_array = array($cat_id);
	while($cat_array[$cat_id][$code]['parent_id'] != 0) {
		$my_cat_array[] = $cat_array[$cat_id][$code]['parent_id'];
		$cat_id = $cat_array[$cat_id][$code]['parent_id'];
	}
	return category_link($my_cat_array[0]);
}

function get_languages() {
	global $db;
	
	$languages = $db->db_query("SELECT languages_id, code FROM ".TABLE_LANGUAGES." ORDER BY sort_order");
	while (!$languages->EOF) {
		$languages_array[] = array ('id' => $languages->fields['languages_id'], 'code' => $languages->fields['code']);
		$languages->MoveNext();
	}
	return $languages_array;
}
$languages = get_languages();

header("Content-type: text/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
echo '<urlset xmlns="http://www.google.com/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.9 http://www.google.com/schemas/sitemap/0.9/sitemap.xsd">'."\n";

for ($i = 0; $i < sizeof($languages); $i ++) {

	# Produkte

	$product = $db->db_query("SELECT 
									p.products_id,
									pd.language_id,
									UNIX_TIMESTAMP(p.products_date_added) AS products_date_added,
									UNIX_TIMESTAMP(p.products_last_modified) AS products_last_modified
								FROM
									".TABLE_PRODUCTS." p,
									".TABLE_PRODUCTS_DESCRIPTION." pd
								WHERE
									p.products_status='1'
								AND 
									p.products_id = pd.products_id
								AND 
									pd.language_id = '".$languages[$i]['id']."'
								ORDER BY 
									p.products_id");
									
	if($product->_numOfRows) {
		while(!$product->EOF) {
			$lang_param = ($languages[$i]['code'] != DEFAULT_LANGUAGE) ? '&language='.$languages[$i]['code'] : '';
			$data .= "\t".'<url>'."\n";
			$data .= "\t\t".'<loc>'.htmlspecialchars(utf8_encode(href_link(FILENAME_PRODUCT_INFO, 'products_id='.$product->fields['products_id'].$lang_param))).'</loc>'."\n";
			$data .= "\t\t".'<priority>0.8</priority>'."\n";
			$data .= "\t\t".'<lastmod>'.date('c', $product->fields['products_date_added']).'</lastmod>'."\n";
			$data .= "\t\t".'<changefreq>daily</changefreq>'."\n";
			$data .= "\t".'</url>'."\n";
			$product->MoveNext();
		}
		echo $data;
	}

	# Kategorien

	$category = $db->db_query("SELECT 
									c.categories_id,
									c.parent_id,
									cd.language_id,
									UNIX_TIMESTAMP(c.date_added) AS date_added,
									UNIX_TIMESTAMP(c.last_modified) AS last_modified
								FROM
									".TABLE_CATEGORIES." c,
									".TABLE_CATEGORIES_DESCRIPTION." cd
								WHERE
									c.categories_status = '1'
								AND 
									c.categories_id = cd.categories_id
								AND 
									cd.language_id = '".$languages[$i]['id']."'
								ORDER by 
									cd.categories_id");

	$cat_array = array();
	if($category->_numOfRows) {
		while(!$category->EOF) {
			$cat_array[$category->fields['categories_id']][$category->fields['code']] = $category->fields;
			$category->MoveNext();
		}
	}
	reset($cat_array);

	foreach($cat_array as $lang_array) {
		foreach($lang_array as $cat_id => $cat_data) {
			$lang_param = ($languages[$i]['code'] != DEFAULT_LANGUAGE) ? '&language='.$languages[$i]['code'] : '';
			$date = ($cat_data['last_modified'] != NULL) ? $cat_data['last_modified'] : $cat_data['date_added'];
			echo "\t".'<url>'."\n";
			echo "\t\t".'<loc>'.href_link(FILENAME_DEFAULT, rv_get_path($cat_data['categories_id'], $cat_data['code']).$lang_param).'</loc>'."\n";
			echo "\t\t".'<priority>0.8</priority>'."\n";
			echo "\t\t".'<lastmod>'.date('c', $date).'</lastmod>'."\n";
			echo "\t\t".'<changefreq>weekly</changefreq>'."\n";
			echo "\t".'</url>'."\n";

		}
	}

	# Content

	$content = $db->db_query("SELECT content_group FROM ".TABLE_CONTENT_MANAGER." WHERE content_status = '1' AND languages_id = '".$languages[$i]['id']."' ");

	while(!$content->EOF) {
		$lang_param = ($languages[$i]['code'] != DEFAULT_LANGUAGE) ? '&language='.$languages[$i]['code'] : '';
		echo "\t".'<url>'."\n";
		echo "\t\t".'<loc>'.htmlspecialchars(utf8_encode(href_link(FILENAME_CONTENT,'coID='.$content->fields['content_group'].$lang_param))).'</loc>'."\n";
		echo "\t\t".'<priority>0.8</priority>'."\n";
		echo "\t\t".'<lastmod>'.date('c', time()).'</lastmod>'."\n";
		echo "\t\t".'<changefreq>weekly</changefreq>'."\n";
		echo "\t".'</url>'."\n";
		$content->Movenext();
	}
} // Ende for()

# Blog
for ($i = 0; $i < sizeof($languages); $i ++) {
	$categories = $db->db_query("SELECT 
									categories_id, 
									titel, 
									UNIX_TIMESTAMP(date) AS date, 
									UNIX_TIMESTAMP(update_date) AS update_date 
								FROM 
									".TABLE_BLOG_CATEGORIES." 
								WHERE 
									status = 2 
								AND 
									language_id = '".$languages[$i]['id']."' 
								ORDER BY 
									position ASC");

	while(!$categories->EOF) {
		$lang_param = ($languages[$i]['code'] != DEFAULT_LANGUAGE) ? '&language='.$languages[$i]['code'] : '';

		echo "\t".'<url>'."\n";
		echo "\t\t".'<loc>'.href_link(FILENAME_BLOG,'blog_cat='.$categories->fields['id'].$lang_param).'</loc>'."\n";
		echo "\t\t".'<priority>0.8</priority>'."\n";
		echo "\t\t".'<lastmod>'.date('c', (!empty($categories->fields['update_date']) ? $categories->fields['update_date'] : $categories->fields['date'])).'</lastmod>'."\n";
		echo "\t\t".'<changefreq>weekly</changefreq>'."\n";
		echo "\t".'</url>'."\n";

		// Einzelne Einträge
		$blog_items = $db->db_query("SELECT 
										item_id, 
										title, 
										UNIX_TIMESTAMP(date) AS date, 
										UNIX_TIMESTAMP(date_update) AS update_date 
									FROM 
										".TABLE_BLOG_ITEMS." 
									WHERE 
										status = 2 
									AND 
										language_id = '".$languages[$i]['id']."' 
									AND 
										categories_id = '".(int)$categories->fields['categories_id']."' 
									ORDER BY 
										position ASC");

		$lang_params = '';

		while(!$blog_items->EOF){
			$lang_param = ($languages[$i]['code'] != DEFAULT_LANGUAGE) ? '&language='.$languages[$i]['code'] : '';

			echo "\t".'<url>'."\n";
			echo "\t\t".'<loc>'.href_link(FILENAME_BLOG, 'blog_cat='.$categories->fields['categories_id'].'&blog_item='.$blog_items->fields['item_id'].$lang_param).'</loc>'."\n";
			echo "\t\t".'<priority>0.8</priority>'."\n";
			echo "\t\t".'<lastmod>'.date('c', (!empty($blog_items->fields['date_update'])) ? $blog_items->fields['date_update'] : $blog_items->fields['date'] ).'</lastmod>'."\n";
			echo "\t\t".'<changefreq>daily</changefreq>'."\n";
			echo "\t".'</url>'."\n";
			$blog_items->MoveNext();
		}
		$categories->MoveNext();
	}
}

echo '</urlset>'."\n";
?>